﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Web;
using System.Xml;
using StattiLibrary;

namespace Stage9.Statti.TwitterLibrary
{
    public class Twitter : IPlugin
    {
        #region IPlugin Members
        private const string StatusesUpdateUrl = "http://twitter.com/statuses/update";
        private const string Format = ".xml";
        private const string PostMethod = "POST";
        private const string ContentType = "application/x-www-form-urlencoded";

        public void Post(string message, string username, string password)
        {
            if (String.IsNullOrEmpty(message))
                return;
            message = HttpUtility.UrlEncode(message);
            HttpWebRequest request = WebRequest.Create(String.Concat(StatusesUpdateUrl, Format)) as HttpWebRequest;
            request.Credentials = new NetworkCredential(username, password);
            request.Proxy = WebRequest.DefaultWebProxy;
            request.Method = PostMethod;
            request.ContentType = ContentType;
            string param = String.Concat("status=", message);
            string sourceParam = String.Concat("&source=", "Voraz");
            request.ContentLength = param.Length + sourceParam.Length;
            StreamWriter writer = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
            writer.Write(param);
            writer.Write(sourceParam);
            writer.Close();
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                XmlDocument responseXML = new XmlDocument();
                responseXML.Load(reader);
                Console.WriteLine(responseXML.ToString());
            }
        }

        public string Name
        {
            get
            {
                return "Twitter Plugin";
            }
        }

        public string Version
        {
            get
            {
                return "1.0.0.0";
            }
        }

        public string Author
        {
            get
            {
                return "Jair Cazarin";
            }
        }

        public string Description
        {
            get
            {
                return "Twitter library";
            }
        }

        #endregion
    }
}
